#include "choosetagsdialog.h"
#include "ui_choosetagsdialog.h"

#include <QCheckBox>
#include <QGridLayout>
#include <QMessageBox>

ChooseTagsDialog::ChooseTagsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChooseTagsDialog)
{
    ui->setupUi(this);

    QStringList tags;
    tags << "原创"
         << "漫画改"
         << "小说改"
         << "游戏改"
         << "特摄"
         << "热血"
         << "穿越"
         << "奇幻"
         << "战斗"
         << "搞笑"
         << "日常"
         << "科幻"
         << "治愈"
         << "校园"
         << "泡面"
         << "恋爱"
         << "少女"
         << "魔法"
         << "冒险"
         << "历史"
         << "架空"
         << "运动"
         << "励志"
         << "音乐"
         << "推理"
         << "社团"
         << "智斗"
         << "催泪"
         << "美食"
         << "偶像"
         << "乙女"
         << "职场";

    int count = tags.count();
    QGridLayout *gridlayout = new QGridLayout(ui->widget);

    int index = 0;
    for(auto p : tags)
    {
        QCheckBox *checkbox = new QCheckBox(p);
        m_boxes.append(checkbox);

        int row = index / 4;
        int col = index % 4;
        index++;

        gridlayout->addWidget(checkbox, row, col);

        connect(checkbox, &QCheckBox::toggled, this, [=](bool toggled){
            if(toggled)
            {
                m_tags.append(checkbox->text());
            }
            else
            {
                m_tags.removeOne(checkbox->text());
            }
        });
    }
}

ChooseTagsDialog::~ChooseTagsDialog()
{
    delete ui;
}

void ChooseTagsDialog::setTags(QStringList tags)
{
    for(auto p : m_boxes)
    {
        p->setChecked(tags.contains(p->text()));
    }
}

QStringList ChooseTagsDialog::getTags()
{
    return m_tags;
}

void ChooseTagsDialog::on_btnOk_clicked()
{
    if(m_tags.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请至少选择一个标签");
        return;
    }

    accept();
}

void ChooseTagsDialog::on_btnCancel_clicked()
{
    reject();
}
